<?php

namespace Duante\GuLu;

use Duante\GuLu\algorithms\AlgorithmsInterface;

class Algorithms implements AlgorithmsInterface
{
    /**
     * GUID
     * @param string $namespace
     * @param bool $separator 是否有 - 拼接
     * @param bool $brace 是否有两侧标签
     * @return string
     */
    public static function createGUID($namespace = '', $separator = false, $brace = false)
    {
        $guid = '';
        $uid = uniqid("", true);
        $data = $namespace;
        $data .= $_SERVER['REQUEST_TIME'];
        $data .= $_SERVER['HTTP_USER_AGENT'];
        $data .= $_SERVER['LOCAL_ADDR'];
        $data .= $_SERVER['LOCAL_PORT'];
        $data .= $_SERVER['REMOTE_ADDR'];
        $data .= $_SERVER['REMOTE_PORT'];
        $hash = strtoupper(hash('ripemd128', $uid . $guid . md5($data)));

        $guid = $hash;
        if (true == $separator) {
            $guid = substr($hash, 0, 8) .
                '-' .
                substr($hash, 8, 4) .
                '-' .
                substr($hash, 12, 4) .
                '-' .
                substr($hash, 16, 4) .
                '-' .
                substr($hash, 20, 12);
        }

        if (true == $brace) {
            $guid = '{' . $guid . '}';
        }

        return $guid;
    }

    /**
     * 创建唯一id
     * @return string
     */
    public static function createNumberID()
    {
        $code = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
        $rand = $code[rand(0, 25)]
            . strtoupper(dechex(date('m')))
            . date('d') . substr(time(), -5)
            . substr(microtime(), 2, 5)
            . sprintf('%02d', rand(0, 99));
        for (
            $a = md5($rand, true),
            $s = '0123456789ABCDEFGHIJKLMNOPQRSTUV',
            $d = '',
            $f = 0;
            $f < 8;
            $g = ord($a[$f]),
            $d .= $s[($g ^ ord($a[$f + 8])) - $g & 0x1F],
            $f++
        ) ;

        return $d;
    }
}